package com.an.blog.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import com.an.blog.admin.domain.BlogArticle;
import com.an.blog.mapper.BlogArticleMapper;
import com.an.blog.service.IBlogCommentService;
import com.an.blog.strategy.SearchStrategy;
import com.an.blog.utils.BlogUniversalUtil;
import com.an.blog.web.domain.bo.BlogArticleWebBo;
import com.an.blog.web.domain.bo.BlogCommentQueryWebBo;
import com.an.blog.web.domain.vo.BlogArticleDetailWebVo;
import com.an.blog.web.domain.vo.BlogArticleWebVo;
import com.an.blog.web.domain.vo.BlogCommentWebVo;
import com.an.common.constant.BlogConstants;
import com.an.common.constant.CacheConstants;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.page.TableDataInfo;
import com.an.system.service.ISysOssService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * MySQL搜索实现
 *
 * @author Ann
 * @date 2024/07/08
 */
@Service("MysqlSearchImpl")
@RequiredArgsConstructor
public class MysqlSearchImpl implements SearchStrategy {

    private final BlogArticleMapper articleMapper;

    private final IBlogCommentService commentService;

    private final ISysOssService ossService;

    @Override
    public TableDataInfo<BlogArticleWebVo> queryWebPageList(BlogArticleWebBo articleWebBo, PageQuery pageQuery) {
        Page<BlogArticle> blogArticlePage = articleMapper.selectArticleWebPageList(pageQuery.build(), articleWebBo);
        //总数
        long total = blogArticlePage.getTotal();
        if (total < 1) {
            return TableDataInfo.build(Collections.emptyList());
        }
        List<BlogArticle> blogArticleList = blogArticlePage.getRecords();
        // 查询tag信息
        List<BlogArticleWebVo> blogArticleWebVos = BlogUniversalUtil.buildArticleWebList(blogArticleList);
        return TableDataInfo.build(blogArticleWebVos, total);
    }

    @Override
    public BlogArticleDetailWebVo queryWebDetailById(Long articleId) {
        BlogArticle blogArticle = articleMapper.selectById(articleId);

        //构建返回前端对象
        BlogArticleDetailWebVo blogArticleDetailWebVo = BeanUtil.toBean(blogArticle, BlogArticleDetailWebVo.class);
        blogArticleDetailWebVo.setArticleImage(ossService.getById(Long.valueOf(blogArticleDetailWebVo.getArticleImage())).getUrl());
        //是否点赞了
        BlogUniversalUtil.isLiked(blogArticleDetailWebVo, CacheConstants.ARTICLE_LIKED_KEY + blogArticle.getArticleId(),
            BlogArticleDetailWebVo::setIsLiked);
        //构建门户需要返回的数据
        BlogUniversalUtil.setArticleWebParameters(blogArticleDetailWebVo, blogArticle.getCategoryId(),
            blogArticle.getArticleType(), blogArticle.getArticleId(), blogArticle.getCreateBy(),
            BlogArticleDetailWebVo::setCategoryName, BlogArticleDetailWebVo::setArticleType, BlogArticleDetailWebVo::setAvatar,
            BlogArticleDetailWebVo::setCreateBy, BlogArticleDetailWebVo::setAuthorId, BlogArticleDetailWebVo::setTags);
        // 查询文章的评论数量
        Long commentCount = commentService.queryCountByTypeAndTopicId(BlogConstants.COMMENT_ARTICLE_TOPIC, articleId);
        blogArticleDetailWebVo.setCommentedCount(commentCount);
        if (commentCount > 0) {
            List<BlogCommentWebVo> blogCommentWebVos = queryArticleCommentList(articleId);
            blogArticleDetailWebVo.setBlogCommentWebVoList(blogCommentWebVos);
        }
        return blogArticleDetailWebVo;
    }

    /**
     * 查询文章评论列表
     *
     * @param articleId 文章编号
     * @return {@link List }<{@link BlogCommentWebVo }>
     * @author Ann
     * @date 2024/06/13
     */
    private List<BlogCommentWebVo> queryArticleCommentList(Long articleId) {
        BlogCommentQueryWebBo blogCommentQueryWebBo = new BlogCommentQueryWebBo();
        blogCommentQueryWebBo.setTopicId(articleId);
        blogCommentQueryWebBo.setTopic(BlogConstants.COMMENT_ARTICLE_TOPIC);
        return commentService.queryWebList(blogCommentQueryWebBo);
    }
}
